package service

import (
	sqlc "bgs/db/sqlc"
	"bgs/util"
	"context"
	"fmt"
)

// ListNormalProductGroupPtrsOfBrand
func (s *ProductServiceImp) ListNormalProductGroupPtrsOfBrand(ctx context.Context, q *sqlc.Queries, brandID int64, pagerPtr *util.Pager) (total int64, ptrs []*sqlc.ProductGroup, err error) {

	total, err = s.dao.Q.CountNormalProductGroupsByBrandID(ctx, brandID)
	if err != nil {
		return
	}
	if total == 0 {
		// 不用继续调用listfunc
		return
	}
	rows, err := s.dao.Q.ListNormalProductGroupsByBrandID(ctx, sqlc.ListNormalProductGroupsByBrandIDParams{
		Limit:   pagerPtr.Limit,
		Offset:  pagerPtr.Offset,
		BrandID: brandID,
	})
	if err != nil {
		return
	}
	ptrs = s.MapProductGroupEntityToPtr(rows)
	return
}

// ListNormalProductGroupPtrsOfDistributor
func (s *ProductServiceImp) ListNormalProductGroupPtrsOfDistributor(ctx context.Context, q *sqlc.Queries, distributorPtr *sqlc.BrandDistributor, pagerPtr *util.Pager) (total int64, ptrs []*sqlc.ProductGroup, err error) {
	if distributorPtr.Status != string(sqlc.EmRecordStatusNormal) {
		err = fmt.Errorf("分销者不可用")
		return
	}
	brandID := distributorPtr.BrandID
	return s.ListNormalProductGroupPtrsOfBrand(ctx, q, brandID, pagerPtr)
}
